/*
 * 
 * 
 * 
 */
package com.hboxs.asl.controller.admin;

import com.hboxs.asl.Message;
import com.hboxs.asl.Page;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.entity.Letter;
import com.hboxs.asl.service.LetterService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpSession;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Controller - 私信
 */
@Controller("adminLetterController")
@RequestMapping("/asl-admin/letter")
public class LetterController extends BaseController {

    @Resource(name = "letterServiceImpl")
    private LetterService letterService;

    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model, HttpSession session) {
        return "/admin/letter/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(Letter letter, RedirectAttributes redirectAttributes) {
        if (!isValid(letter)) {
            return ERROR_VIEW;
        }
        letterService.save(letter);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model) {
        Letter letter = letterService.find(id);
        Set<Letter> letterSet = letter.getReplyLetteres();
//        List<Letter> letterList = letterService.findLetterDetailList(letter);
        List<Letter> letterList = new ArrayList<>(letterSet);
        model.addAttribute("letter", letter);
        model.addAttribute("letterList", letterList);
        return "/admin/letter/edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Letter letter, RedirectAttributes redirectAttributes) {
        if (!isValid(letter)) {
            return ERROR_VIEW;
        }
        letterService.update(letter, "");
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Pageable pageable, ModelMap model) {
        Page<Letter> page = letterService.findPageLetters(pageable);
        model.addAttribute("page", page);
        return "/admin/letter/list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    Message delete(Long[] ids) {
        letterService.delete(ids);
        return SUCCESS_MESSAGE;
    }

}
